import { Context } from "koa";
import Router from "koa-router";
import {
  ITokenOptions,
  jwtAuth,
  getToken,
  requestRefreshToken,
} from "./controller";

export interface IAuthOptions extends ITokenOptions {
  usecret: string;
  client: string;
  server: string;
}

export function getRouter(options: IAuthOptions): Router {
  const router = new Router();

  router.get("/oauth", async (ctx: Context) => {
    ctx.body = {
      client: options.client,
      server: options.server,
    };
  });

  router.get("/test", jwtAuth(options.secret), async (ctx: Context) => {
    ctx.body = { success: true };
  });

  router.get("/token", jwtAuth(options.usecret), getToken(options));

  router.get(
    "/refresh",
    jwtAuth(options.secret),
    requestRefreshToken,
    getToken(options)
  );
  return router;
}
